import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:lianwenke20220712/my.dart';
import 'package:lianwenke20220712/tab.dart';

/// FileName 连文科
///
/// @Author/// @Date 2022/7/12 14:12
///
/// @Description 首页底部导航栏
class HomePageWidget extends StatefulWidget {
  const HomePageWidget({Key key}) : super(key: key);

  @override
  _HomePageWidgetState createState() => _HomePageWidgetState();
}

class _HomePageWidgetState extends State<HomePageWidget> {
  PageController _controller;
  List<Widget> _list;
  int _currentIndex = 0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = PageController();
    _list = [
      Container(color: Colors.blueGrey,child: TabWidget(),),
      Container(color: Colors.deepOrangeAccent,),
      Container(color: Colors.brown,),
      Container(color: Colors.blue,child: MyWidget(),),
    ];

    Connectivity().onConnectivityChanged.listen((event) {
      if(event == ConnectivityResult.none){
        Fluttertoast.showToast(msg: '已断网');
      }
    });
  }
  //避免内存泄漏
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _controller.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _controller,
        onPageChanged: (index){
          setState(() {
            _currentIndex = index;
          });
        },
        children: _list,
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index){
          setState(() {
            _currentIndex = index;
          });
          _controller.jumpToPage(index);
        },
        type: BottomNavigationBarType.fixed,
        items: [
          BottomNavigationBarItem(icon: Image.asset('assets/images/sy.png',),
          label: '首页'),
          BottomNavigationBarItem(icon: Image.asset('assets/images/fx.png',),
              label: '发现'),
          BottomNavigationBarItem(icon: Image.asset('assets/images/xx.png',),
              label: '消息'),
          BottomNavigationBarItem(icon: Image.asset('assets/images/wd.png',),
              label: '我的'),
        ],
      ),
    );
  }
}
